// miniprogram/pages/my/my_order/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: ["全部", "未完成", "已完成", "退款中", "已关闭"],
    datas: [],
    sitchHead: false,
    type: 0
  },
  orderState: "",
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      this.setData({
        type: options.type
      });
      console.info("state:" + options.type);

      this.changeType(parseInt(options.type));
    } else {
      this.init();
    }
  },
  change(e) {
    let tab = e.detail.value;
    this.changeType(tab);
    if (tab) {
      this.setData({
        sitchHead: true,
      })
      return;
    }

    return this.setData({
      sitchHead: false,
    })

  },
  changeType(tab) {
    console.info("state:" + tab);

    var index = this.data.options[tab];
    var states = {};
    states["全部"] = "";
    states["未完成"] = "waitPay";
    states["已完成"] = "complete";
    states["退款中"] = "refund";
    states["已关闭"] = "cancel";

    var state = states[index];
    if (state) {
      this.orderState = state;
    } else {
      this.orderState = "";
    }

    this.init();
  },
  init() {
    this.no = 1;
    var params = {};
    params.no = this.no;
    params.size = 10;
    params.orderState = this.orderState;
    app.dataRequest("/rest/orderform/searchByMy.htm", params, this, "orderList");
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.info(this.totalPage);
    if (this.no < this.totalPage) {
      this.no = this.no + 1;
      this.setData({
        noMore: false
      });
      var params = {};
      params.no = this.no;
      params.size = 10;
      params.orderState = this.orderState;
      app.loadMore("/rest/orderform/searchByMy.htm", params, this, "orderList");

    } else {
      this.setData({
        noMore: true
      });
    }
  },
  payOrder(e) {
    var obj = e.currentTarget.dataset.obj;
    var param={};
    param.id=obj.id;
    app.postRequestLoading("/rest/orderform/rePay.htm", param, this.backOrder);


  },
  backOrder(res) {
    if (res.code == 0) {
      app.pay(res.pay, this.paySuccess);
    } else {
      app.showModal(res.msg);
    }
  },
  paySuccess() {
    this.init();
  },
})